﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmEditKTVOrder : BaseForm
    {
        public KTVSuiteInfo RoomInfo = null;

        public FrmEditKTVOrder()
        {
            InitializeComponent();

            InitDictItems();
        }

        private void InitDictItems()
        {
            this.txtCaculateType.Items.Clear();
            List<KTVCaculteTypeInfo> typeList = BLLFactory<KTVCaculteType>.Instance.GetAll();
            foreach (KTVCaculteTypeInfo typeInfo in typeList)
            {
                this.txtCaculateType.Items.Add(new CListItem(typeInfo.Name, typeInfo.ID.ToString()));
            }
            if (this.txtCaculateType.Items.Count > 0)
            {
                this.txtCaculateType.SelectedIndex = 0;
            }
        }

        private void FrmEditOrder_Load(object sender, EventArgs e)
        {
            if (RoomInfo != null)
            {
                this.lblRoomNo.Text = RoomInfo.RoomNo;
                this.lblRoomType.Text = RoomInfo.RoomType;

                KTVSuiteTypeInfo typeInfo = BLLFactory<KTVSuiteType>.Instance.FindByName(RoomInfo.RoomType);
                if (typeInfo != null)
                {
                    this.lblAtLeaseMoney.Text = typeInfo.AtLeaseMoney.ToString("f2");
                    this.txtCaculateType.Text = typeInfo.CaculateType;
                    this.txtPrice.Text = typeInfo.Price.ToString("f2");
                    this.txtBuyOutPrice.Text = typeInfo.BuyOutPrice.ToString("f2");
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                KTVOrderInfo orderInfo = new KTVOrderInfo();
                orderInfo.CreateDate = DateTime.Now;
                orderInfo.Createor = Portal.gc.LoginInfo.FullName;
                orderInfo.Note = this.txtNote.Text;
                orderInfo.OrderNo = this.txtOrderNo.Text;
                orderInfo.IsCheckOut = false;

                orderInfo.AtLeaseMoney = Convert.ToDecimal(this.lblAtLeaseMoney.Text);
                orderInfo.DepositMoney = Convert.ToDecimal(this.txtDeposit.Text);
                orderInfo.CaculateType = this.txtCaculateType.Text;
                orderInfo.Price = Convert.ToDecimal(this.txtPrice.Text);
                orderInfo.BuyOutPrice = Convert.ToDecimal(this.txtBuyOutPrice.Text);
                orderInfo.RoomType = this.lblRoomType.Text;
                orderInfo.RoomNo = this.lblRoomNo.Text;//房间号
                orderInfo.Persons = Convert.ToInt16(this.txtPersons.Text);
                orderInfo.Waiter = "*";
                orderInfo.Note = "";
                orderInfo.IsBuyOut = this.chkBuyOut.Checked;//是否是买断消费

                int orderID = BLLFactory<KTVOrder>.Instance.Insert2(orderInfo);
                decimal buyOutMoney = Convert.ToDecimal(this.txtBuyOutPrice.Text);
                //if (this.chkBuyOut.Checked && buyOutMoney > 0)
                //{
                //    InsertBuyOutConsumption(orderID, buyOutMoney);
                //}

                MessageUtil.ShowTips("保存订单成功");
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageUtil.ShowError("保存订单出错：" + ex.Message);
            }
        }

        /// <summary>
        /// 写入买断消费清单
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="buyOutMoney"></param>
        /// <returns></returns>
        private bool InsertBuyOutConsumption(int orderID, decimal buyOutMoney)
        {
            bool result = false;
            if (orderID > 0)
            {
                KTVConsumerListInfo conInfo = new KTVConsumerListInfo();
                conInfo.ItemType = "房间买断消费";
                conInfo.ItemName = "房间买断消费";
                conInfo.Price = buyOutMoney;
                conInfo.Quantity = 1;
                conInfo.RoomNo = RoomInfo.RoomNo;
                conInfo.Waiter = "*";
                conInfo.DiscountPrice = buyOutMoney;
                conInfo.Amount = conInfo.Price * 1;
                conInfo.BeginTime = DateTime.Now;
                conInfo.Creator = Portal.gc.LoginInfo.FullName;
                conInfo.Discount = 1.0;
                conInfo.Order_ID = orderID;

                result = BLLFactory<KTVConsumerList>.Instance.Insert(conInfo);
            }
            return result;
        }

        private void txtCaculateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = this.txtCaculateType.SelectedItem as CListItem;
            if (item != null && !string.IsNullOrEmpty(item.Value))
            {
                KTVCaculteTypeInfo typeInfo = BLLFactory<KTVCaculteType>.Instance.FindByID(item.Value);
                if (typeInfo != null)
                {
                    this.txtPrice.Text = typeInfo.Price.ToString("f2");
                }
            }
        }
    }
}
